<!--
 * @Author: your name
 * @Date: 2021-11-10 15:30:56
 * @LastEditTime: 2021-12-04 08:57:26
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\admin\src\views\home\index.vue
-->
<template>
  <div class="home">
    <el-container>
      <el-aside width="" style="height: 100%; ackground-color: #001529">
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical-demo"
          :collapse="isCollapse"
          background-color="#001529"
          text-color="#fff"
          style="height: 100%"
          router
          unique-opened
          :collapse-transition="true"
        >
          <template>
            <div class="logo-box">
              <img :src="logoImg" alt="" />
              <span v-if="!isCollapse">管理后台</span>
            </div>
            <div class="newCreate-box">
              <el-button v-if="!isCollapse">+ 新建</el-button>
              <el-button v-else>+</el-button>
              <ul class="new-menu">
                <li
                  v-for="(item, index) in createRoutes"
                  :key="index"
                  @click="
                    () => {
                      $router.push(item.path);
                    }
                  "
                >
                  {{ item.text }}
                </li>
              </ul>
            </div>
          </template>
          <template v-for="(item, index) in routerList">
            <template v-if="item.children">
              <el-submenu index="" :key="index">
                <template slot="title">
                  <i :class="`iconfont ${item.icon}`"></i>
                  <span slot="title">{{ item.text }}</span>
                </template>
                <el-menu-item
                  :index="val.path"
                  v-for="(val, ind) in item.children"
                  :key="ind"
                >
                  <i :class="`iconfont ${val.icon}`"></i>
                  <span slot="title">{{ val.text }}</span>
                </el-menu-item>
              </el-submenu>
            </template>
            <el-menu-item :index="item.path" :key="index" v-else>
              <i :class="`iconfont ${item.icon}`"></i>
              <span slot="title">{{ item.text }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header style="height: auto">
          <div class="openIcon-box">
            <template v-if="isCollapse">
              <i
                class="iconfont icon-shouqi1"
                @click="changeOpen()"
                style="font-size: 16px"
              />
            </template>
            <template v-else>
              <i
                class="iconfont icon-zhankai"
                @click="changeOpen()"
                style="font-size: 18px"
              />
            </template>
            <div class="menage-header-box">
              <a href="https://github.com/fantasticit/wipi" target="_blank"
                ><i
                  class="iconfont icon-github"
                  style="font-size: 17px; color: #000"
                ></i>
              </a>
              <el-dropdown>
                <span class="el-dropdown-link">
                  <img
                    :src="avatar"
                    alt=""
                    style="width: 20px; border-radius: 50%; height: 20px"
                  />
                  <span>Hi，{{ username }}</span>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    v-for="(item, index) in dropdown"
                    :key="index"
                    @click.native="gotoUrl(item.path)"
                    >{{ item.text }}</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <template v-if="pathname !== '/personalCenter'">
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/workbench' }"
                >工作台</el-breadcrumb-item
              >
              <el-breadcrumb-item
                v-for="(item, index) in breadList"
                :key="index"
                :to="{ path: `${item.path}` }"
                >{{ item.text }}</el-breadcrumb-item
              >
            </el-breadcrumb>
          </template>
        </el-header>
        <el-main><router-view /></el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import router from "../../router/routerList";
import logo from "../../assets/1.webp";
import noImg from "../../assets/头像.webp";
import { createRoutes, dropdown } from "../../data/home";
import addBreadcrumbData from "../../utils/addBreadcrumbData";
import { mapState, mapMutations } from "vuex";
import api from "../../api/users";
export default {
  computed: {
    //导航高亮跟随
    ...mapState(["home", ["Routers"]]),
    ...mapState(["refreshNum"]),
    activeMenu() {
      const route = this.$route;
      const { meta, path } = route;
      // if set path, the sidebar will highlight the path you set
      // 可以在路由配置文件中设置自定义的路由路径到meta.activeMenu属性中，来控制菜单自定义高亮显示
      if (meta.activeMenu) {
        return meta.activeMenu;
      }
      return path;
    },
    //控制面包屑显隐
    pathname() {
      const route = this.$route;
      const { path } = route;
      return path;
    },
  },
  data() {
    return {
      isCollapse: false, //是否收起
      logoImg: logo,
      avatar: "",
      userId: null,
      createRoutes, //
      username: "", //name
      dropdown, //用户下拉菜单
      breadList: [],
      routerList: [],
    };
  },
  methods: {
    ...mapMutations(["addRefNum"]),
    //改变侧边栏状态
    changeOpen() {
      this.isCollapse = !this.isCollapse;
    },
    //跳转路径
    gotoUrl(path) {
      this.$router.push(path);
    },
    //管理员路由
    getRouterListAdmin() {
      this.routerList = this.routerList.find((item) => {
        if (item.children) {
          return item;
        }
      });
      this.routerList = this.routerList.children;
      this.routerList.splice(0, 1);
      this.routerList.splice(12, 1);
      this.routerList[1].children.splice(0, 1);
      if (this.refreshNum <= 1) {
        window.localStorage.setItem(
          "routerList",
          JSON.stringify(this.routerList)
        );
      }
    },
    //用户路由
    getRouterListClient() {
      this.routerList = this.routerList.find((item) => {
        if (item.children) {
          return item;
        }
      });
      this.routerList = this.routerList.children;
      this.routerList.splice(0, 1);
      this.routerList.splice(7, 1);
      this.routerList[1].children.splice(0, 1);
      this.resultList = this.routerList;
      if (this.refreshNum <= 1) {
        window.localStorage.setItem(
          "routerList",
          JSON.stringify(this.routerList)
        );
      }
    },
    async getUserInfo() {
      const res = await api.getUserInfo({ id: this.userId });
      if (res.data.avatar) {
        this.avatar = res.data.avatar;
      } else {
        this.avatar = noImg;
      }
    },
  },
  //监听
  watch: {
    //pathname跳转路径
    pathname() {
      this.breadList = [];
      const res = addBreadcrumbData(router, this.pathname);
      if (res) {
        window.localStorage.setItem("breadList", JSON.stringify(res));
        this.breadList = JSON.parse(window.localStorage.getItem("breadList"));
      } else {
        window.localStorage.removeItem("breadList");
      }
    },
  },
  created() {
    this.addRefNum();
    const userData = JSON.parse(window.localStorage.getItem("userData"));
    const role = userData.identity;
    this.userId = userData.id;
    this.getUserInfo();
    this.username = userData.nickname;
    this.breadList = JSON.parse(window.localStorage.getItem("breadList"));
    //获取用户信息
    if (this.refreshNum <= 1) {
      if (role === "管理员") {
        this.routerList = this.Routers;
        this.getRouterListAdmin();
      } else {
        this.routerList = this.Routers;
        this.getRouterListClient();
      }
    } else {
      this.routerList = JSON.parse(window.localStorage.getItem("routerList"));
    }
  },
};
</script>

<style lang="scss" scoped>
.el-main {
  background-color: #eff2f5;
  padding: 0;
}
//滚动条样式
.el-main::-webkit-scrollbar {
  width: 5px !important;
}
//滚动条里面小方块样式
.el-main::-webkit-scrollbar-thumb {
  border-radius: 5px;
  -webkit-box-shadow: inset 0 0 5px rgba(218, 218, 218, 0.2);
  background: rgba(0, 0, 0, 0.2);
}
.el-aside {
  overflow: none;
}
.new-menu {
  width: 158px;
  background-color: #fff;
  position: absolute;
  top: 103px;
  left: 21px;
  z-index: 9;
  border-radius: 2px;
  display: none;
  li {
    padding: 8px 5px;
    font-size: 14px;
    cursor: pointer;
  }
  li:hover {
    background-color: #f5f5f5;
  }
}
.home,
.el-container {
  width: 100%;
  height: 100%;
}
.el-header {
  padding: 0;
  .iconfont {
    font-size: 14px;
    cursor: pointer;
  }
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-aside::-webkit-scrollbar {
  width: 0 !important;
}
.openIcon-box {
  width: 100%;
  height: 60px;
  display: flex;
  padding: 0 20px;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  border-bottom: 1px solid rgb(233, 233, 233);
}
.logo-box {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 15px 0;
  color: #fff;
  span {
    flex-shrink: 0;
  }
  img {
    width: 35px;
    height: 35px;
    border-radius: 5px;
    margin-right: 10px;
  }
}
.iconfont {
  font-size: 16px;
  margin-right: 10px;
}
.newCreate-box {
  width: 100%;
  display: flex;
  justify-content: center;
  button {
    width: 80%;
    background-color: #0188fb;
    border: none;
    color: #fff;
    flex-shrink: 1;
    span {
      width: 100%;
      height: 100%;
    }
  }
}
.newCreate-box:hover {
  .new-menu {
    display: block;
  }
}
.menage-header-box {
  display: flex;
  align-content: center;
  font-size: 14px;
  img {
    margin: 0 10px;
    margin-left: 4px;
  }
}
.el-breadcrumb {
  display: flex;
  width: 100%;
  height: 50px;
  align-items: center;
  padding: 0 20px;
  // cursor: pointer;
}
.el-dropdown-selfdefine {
  display: flex;
  align-items: center;
  cursor: pointer;
}
</style>
